package com.yulongtian.weekCompetition.month202302.week03;

import java.util.Arrays;

/**
 *
 * @author yulongTian
 * @create 2023-02-19 10:51
 */
public class Test02 {
    public static void main(String[] args) {
//        for (int i = 0; i < 32; i++) {
//            System.out.println("i =" + i);
//            System.out.println(Math.pow(2, i));
//            if (Math.pow(2, i) > 100000) {
//                break;
//            }
//            System.out.println();
//        }
        System.out.println(minOperations(9));
    }

    public static int minOperations(int n) {
        int[] nums = new int[18];
        for (int i = 0; i < 18; i++) {
            nums[i] = (int) Math.pow(2, i);
        }
        int ans = 0;
        while (n > 0) {
            if (n == 1) {
                ans++;
                break;
            }
            int i = 1;
            //距离谁最近
            for (; i < 18; i++) {
                if (nums[i] >= n && nums[i - 1] <= n) {
                    break;
                }
            }

            int target = n - nums[i - 1] >= nums[i] - n ? nums[i] : nums[i - 1];
            if (target <= n) {
                n -= target;
                ans++;
            } else {

            }
        }
        return ans;
    }
}
